Service.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. "use client";
  2. import { ServiceTypes } from "@/api/customservice";
  3. import { getPaysApi, lredPacketApi, PayDataType, redPacketApi } from "@/api/promo";
  4. import RedPacketModal, { RedPacketModalProps } from "@/components/Box/RedPacketModal";
  5. import SlotsModal, { SlotModalRefProps } from "@/components/Box/SlotsModal";
  6. import UserRecharge, { ModalRefProps, Timeout } from "@/components/Box/UserRecharge";
  7. import WheelModal, { WheelModalProps } from "@/components/Box/WheelModal";
  8. import { getWheelApi } from "@/api/cashWheel";
  9. import { getGiveInfoApi } from "@/api/slots";
  10. import { Link } from "@/i18n/routing";
  11. import { useGlobalNoticeStore } from "@/stores/useGlobalNoticeStore";
  12. import { useSocialStore } from "@/stores/useSocials";
  13. import useWheelStore from "@/stores/useWheelStore";
  14. import { getToken } from "@/utils/Cookies";
  15. import { useRequest } from "ahooks";
  16. import { Badge } from "antd-mobile";
  17. import { useTranslations } from "next-intl";
  18. import Image from "next/image";
  19. import { FC, useEffect, useRef } from "react";
  20. interface Props {
  21. services: ServiceTypes[];
  22. socials: ServiceTypes[];
  23. }
  24. const ServiceWidget: FC<Props> = (props) => {
  25. const token = getToken();
  26. const { services, socials = [] } = props;
  27. const { wheelStatus, wheelCurrent, setWheel } = useWheelStore((state) => ({
  28. wheelStatus: state.status,
  29. wheelCurrent: state.currentWheel,
  30. setWheel: state.setWheel,
  31. }));
  32. const defaultService = services?.find((item) => item.is_suspend === 1);
  33. const newServices = services?.filter((item) => item.status === 1) || [];
  34. const setSocials = useSocialStore((state) => state.setSocials);
  35. const redPacketModalRef = useRef<RedPacketModalProps>(null);
  36. /// 首充活动ref
  37. const userRechargeRef = useRef<ModalRefProps | null>(null);
  38. // 轮盘
  39. const wheelModalRef = useRef<WheelModalProps | null>(null);
  40. const slotsRef = useRef<SlotModalRefProps | null>(null);
  41. const getWheel = () => {
  42. if (!getToken()) return Promise.resolve(undefined);
  43. return getWheelApi().then((res) => {
  44. return res.data;
  45. });
  46. };
  47. useEffect(() => {
  48. // 数据存储,侧边栏使用
  49. setSocials(socials);
  50. setWheel().then((data) => {
  51. if (data && useWheelStore.getState().status === 1) {
  52. wheelModalRef.current?.onOpen(data);
  53. }
  54. });
  55. }, []);
  56. const t = useTranslations("HomePage");
  57. const { unread, userUnred } = useGlobalNoticeStore((state) => ({
  58. unread: state.unread,
  59. userUnred: state.userUnred,
  60. }));
  61. const getRedPacketInfo = async () => {
  62. try {
  63. let redPacketInfo: any;
  64. let actList: any = [];
  65. if (token) {
  66. redPacketInfo = await lredPacketApi();
  67. actList = redPacketInfo.data?.red_packets || [];
  68. } else {
  69. redPacketInfo = await redPacketApi();
  70. actList = redPacketInfo.data || [];
  71. }
  72. // 是否有已开始但是没领过的红包
  73. return actList.filter((aItem: any) => {
  74. return aItem.can_receive && aItem.is_start && !aItem.is_receive;
  75. });
  76. } catch (error) {
  77. console.log("redPacketInfo===>error:", error);
  78. }
  79. };
  80. // 红包雨轮询
  81. const { data: packets, run } = useRequest<any[], any>(getRedPacketInfo, {
  82. pollingInterval: 5000,
  83. pollingErrorRetryCount: 1,
  84. pollingWhenHidden: false,
  85. });
  86. // 首充活动
  87. const getPayInfo = async (): Promise<PayDataType> => {
  88. if (token) {
  89. const result = await getPaysApi();
  90. return result.data;
  91. } else {
  92. return Promise.resolve({
  93. first_pay: [],
  94. pay: [],
  95. });
  96. }
  97. };
  98. const { data: paysInfo, run: payRun } = useRequest<PayDataType, any>(getPayInfo, {
  99. pollingErrorRetryCount: 1,
  100. pollingWhenHidden: false,
  101. });
  102. /**
  103. * 免费送活动
  104. */
  105. const getSlots = async () => {
  106. if (token) {
  107. const result = await getGiveInfoApi();
  108. return result.data;
  109. } else {
  110. return Promise.resolve({});
  111. }
  112. };
  113. const { data: slots, run: slotRun } = useRequest<any, any>(getSlots, {
  114. pollingErrorRetryCount: 1,
  115. pollingWhenHidden: false,
  116. });
  117. const slotHandler = () => {
  118. slotsRef.current?.onOpen(slots);
  119. };
  120. return (
  121. <>
  122. <div
  123. className={`absolute bottom-[0.84rem] right-[0.12rem] z-50 flex w-[0.6944rem] flex-col items-center justify-center`}
  124. >
  125. {slots?.id ? (
  126. <img
  127. src={"/slots/slots-icon.gif"}
  128. className={"mb-[0.2778rem] w-[100%]"}
  129. onClick={slotHandler}
  130. />
  131. ) : null}
  132. {/*轮盘 */}
  133. {wheelStatus === 2 ? (
  134. <Link
  135. href={"/cashWheel"}
  136. className={
  137. "mb-[0.2778rem] flex h-[0.54rem] w-[0.54rem] items-center" +
  138. " justify-center rounded-[50%] bg-gradient-to-b from-[#0575e6] to-[#00f260]"
  139. }
  140. >
  141. <Image
  142. src={"/wheels/wheel-icon.gif"}
  143. alt={"wheel"}
  144. width={100}
  145. height={100}
  146. />
  147. </Link>
  148. ) : null}
  149. {/*首充*/}
  150. {paysInfo?.first_pay?.map((item, index) => {
  151. return (
  152. <div
  153. key={index}
  154. className={`mb-[0.2778rem] flex cursor-pointer flex-col items-center`}
  155. >
  156. <img
  157. className={"w-[0.54rem]"}
  158. src="/hby/recharge.png"
  159. onClick={() => {
  160. // @ts-ignore
  161. userRechargeRef.current?.onOpen(paysInfo.first_pay, index);
  162. }}
  163. />
  164. {item.end_time > 0 ? (
  165. <Timeout
  166. className={
  167. "relative before:left-0 before:top-0 before:content-['']" +
  168. " -m-[0.0417rem] before:h-[100%] before:blur-[0.0486rem]" +
  169. " before:absolute before:-z-10 before:w-[100%] before:bg-[#ed9d00]" +
  170. " text-[0.08rem]" +
  171. " z-1 text-[#fff]"
  172. }
  173. endTime={item.end_time}
  174. onEndHandler={payRun}
  175. />
  176. ) : null}
  177. </div>
  178. );
  179. })}
  180. {/* 红包雨icon */}
  181. {packets?.map((item, index) => {
  182. return (
  183. <div key={index} className={`mb-[0.2778rem] cursor-pointer`}>
  184. <img
  185. className={"w-[0.54rem] object-fill"}
  186. src="/hby/red-icon.gif"
  187. onClick={() => {
  188. redPacketModalRef.current?.onOpen(packets, index);
  189. }}
  190. />
  191. </div>
  192. );
  193. })}
  194. {/*未读消息*/}
  195. {unread || userUnred ? (
  196. <Link
  197. href={"/notification"}
  198. className={
  199. "mb-[0.2778rem] flex h-[0.54rem] w-[0.54rem] items-center" +
  200. " justify-center rounded-[50%] bg-gradient-to-t from-[#ffa111]" +
  201. " to-[#ffcf35]"
  202. }
  203. >
  204. <Badge content={userUnred + unread} style={{ "--top": "12px" }}>
  205. <i
  206. className={"iconfont icon-duanxinguanli text-[0.3rem] text-[#fff]"}
  207. ></i>
  208. </Badge>
  209. </Link>
  210. ) : null}
  211. {defaultService && (
  212. <Link
  213. href={defaultService.url}
  214. className={
  215. "flex h-[0.54rem] w-[0.54rem] items-center justify-center rounded-[50%]" +
  216. " bg-gradient-to-t from-[#ff611b] to-[#ffcf35]"
  217. }
  218. target={"_blank"}
  219. >
  220. <img
  221. className={"h-[0.3889rem] w-[0.3889rem] object-contain"}
  222. src={defaultService.icon_url}
  223. alt={""}
  224. ></img>
  225. </Link>
  226. )}
  227. </div>
  228. {/*红包雨弹窗*/}
  229. <RedPacketModal ref={redPacketModalRef} onAfterHandler={run} />
  230. {/*首充弹窗*/}
  231. <UserRecharge ref={userRechargeRef} />
  232. {/* 轮盘弹窗 */}
  233. <WheelModal ref={wheelModalRef} />
  234. {/*随机送*/}
  235. <SlotsModal ref={slotsRef} onAfterHandler={slotRun} />
  236. <div
  237. className={`grid`}
  238. style={{
  239. gridTemplateColumns: ` repeat(${newServices && newServices.length >= 5 ? 5 : (newServices?.length ?? 1)},1fr)`,
  240. }}
  241. >
  242. {newServices.map((service, index) => {
  243. return (
  244. <Link
  245. key={index}
  246. href={service.url}
  247. target={"_blank"}
  248. className="bg-white m-[0.05rem] h-[0.3889rem] w-[0.3889rem] rounded"
  249. >
  250. <img
  251. className={"h-[0.3889rem] w-[0.3889rem]"}
  252. src={service.icon_url}
  253. ></img>
  254. </Link>
  255. );
  256. })}
  257. </div>
  258. <div className={"text-[#ff6a01]"}>{t("Service")}</div>
  259. {/*share*/}
  260. <div className={"my-[0.2rem] text-[0.08rem] text-[#adadad]"}>{t("Share")}</div>
  261. <div
  262. className={`grid`}
  263. style={{
  264. gridTemplateColumns: ` repeat(${socials.length >= 5 ? 5 : socials.length},1fr)`,
  265. }}
  266. >
  267. {socials.map((service, index) => {
  268. return (
  269. <Link
  270. key={index}
  271. href={service.url}
  272. target={"_blank"}
  273. className="bg-white m-[0.05rem] h-[0.3889rem] w-[0.3889rem] rounded"
  274. >
  275. <img
  276. className={"h-[0.3889rem] w-[0.3889rem]"}
  277. src={service.icon_url}
  278. ></img>
  279. </Link>
  280. );
  281. })}
  282. </div>
  283. </>
  284. );
  285. };
  286. export default ServiceWidget;